home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: What is '?' in C mean....?????
- Date: 9 Jan 1996 08:21:04 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4ct8hg$mtl@sparcserver.lrz-muenchen.de>
- References: <4cgsa8$bm2@wumpus.cc.uow.edu.au> <fcusack-0401961115540001@mudskipper.cac.psu.edu> <4ci5bb$8m4@www.gnofn.org>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- clm01@www.gnofn.org (Christopher L Mayeux) writes:
-
- > frank. (fcusack@tdx.org) wrote:
- >: > Could anyone here explain to me what is "?" means and what the purpose
- >: of using
- >:
- >: ? : is C's ternary operator. if the condition is met, perform the
- >: operation after the ?; if the condition is not met, perform the operation
- >: after the :
- >:
-
- >What new perversion of C is that ???
-
- >I've been programming in standard C for 12 years, and
- >never saw THAT in the manuals.
-
- If both the "operation" before the colon and the "operation" behind the
- colon are expressions that are evaluated only for their side effects,
- the wording is still poor but you _can_ do something like
-
- x > y ?
- printf("x is greater than y") :
- printf("x is not greater than y");
-
- An expressin that uses C's only ternary operator is called a conditional
- expression, and conditional expressions make a lot of sense, esp. if you
- try to write C functions in the style of a functional programming language.
-
- unsigned int fak(unsigned int n) { return x < 2 ? 1 : n * fak(n - 1); }
-
- or if the conditional expression is part of a large expression.
-
- Conditional expressions where part of the C programming language
- at least since K&R 1. I would not call them perversions at all,
- but you can be sure that they are no _new_ perversions.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-